const {
  ActivityModel
} =  require('../../../../../utils/activity-model.js')
const activityModel = new ActivityModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationHeight: 64,
    navigationTop: 26,
    navigationLeft: 10,
    capsuleWidth: 87,
    ruleShow: false,
    medals: [],
    deleteResult: {}
  },
  closeRule() {
    this.setData({
      ruleShow: false
    })
    if (!this.data.medals.length) {
      this.goback()
    }
  },
  startDelete(e) {
    const { item } = e.currentTarget.dataset
    activityModel.dustRepeatMedal({
      activityId: this.data.activityId,
      smashAll: false,
      shopId: item.shopId
    }).then(data => {
      this.setData({
        ruleShow: true,
        deleteResult: data || {}
      })
      this.getLists()
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  deleteAll() {
    activityModel.dustRepeatMedal({
      activityId: this.data.activityId,
      smashAll: true
    }).then(data => {
      this.setData({
        ruleShow: true,
        deleteResult: data || {}
      })
      this.getLists()
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  goback() {
    wx.navigateBack({
      fail() {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    })
  },
  onLoad: function (options) {
    this.setData({
      activityId: options.activityId
    })
    const navigationBtnObj = wx.getMenuButtonBoundingClientRect()
    wx.getSystemInfo({
      success: res => {
        this.setData({
          navigationHeight: res.statusBarHeight + navigationBtnObj.height + (navigationBtnObj.top - res.statusBarHeight) * 2,
          navigationTop: navigationBtnObj.top,
          navigationLeft: res.windowWidth - navigationBtnObj.right,
          capsuleWidth: navigationBtnObj.width
        })
      }
    })
    this.getLists()
  },
  getLists() {
    activityModel.getRepeatMedal({
      activityId: this.data.activityId
    }).then(data => {
      const medals = (data || []).map(item => {
        item.badges--
        return item
      }).filter(item => !!item.badges)
      this.setData({
        medals
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  }
})